package com.huluobo.a2110mvpimdemo.addGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2110mvpimdemo.R;
import com.huluobo.a2110mvpimdemo.adapter.AddGroupAdapter;
import com.huluobo.a2110mvpimdemo.adapter.FindJoinedGroupAdapter;
import com.huluobo.a2110mvpimdemo.base.BaseActivity;
import com.huluobo.a2110mvpimdemo.chat.ChatActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseActivity<AddGroupContract.View, AddGroupPresenter>
        implements AddGroupContract.View {

    private EditText groupName, groupDesc;

    private Button createGroupBtn, findPublicGroupBtn, findJoinedGroupBtn;
    private RecyclerView recyclerView;
    private AddGroupAdapter addGroupAdapter;
    private List<EMGroupInfo> groupInfoList = new ArrayList<>();
    private FindJoinedGroupAdapter findJoinedGroupAdapter;

    private List<EMGroup> groupList = new ArrayList<>();


    @Override
    public void onCreateGroupSuccess() {
        runOnUiThread(() -> {
            Toast.makeText(this, "创建群成功", Toast.LENGTH_SHORT).show();
            groupName.setText("");
            groupDesc.setText("");
        });
    }

    @Override
    public void onCreateGroupFailed(String error) {
        runOnUiThread(() -> {
            Toast.makeText(this, "创建群失败:" + error, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void findPublicGroupsSuccess(List<EMGroupInfo> list) {
        Log.i("TagA", "查找群:" + list);
        runOnUiThread(() -> {
            groupInfoList.clear();
            groupInfoList.addAll(list);
            recyclerView.setAdapter(addGroupAdapter);
        });
    }

    @Override
    public void findPublicGroupsFailed(String error) {
        Log.i("TagA", "查找公开群失败:" + error);
    }

    @Override
    public void findJoinedGroupsSuccess(List<EMGroup> list) {
        runOnUiThread(() -> {
            Log.i("TagA", "查找已经加入的群:" + list);
            groupList.clear();
            groupList.addAll(list);
            recyclerView.setAdapter(findJoinedGroupAdapter);
        });

    }

    @Override
    public void findJoinedGroupsFailed(String error) {
        Log.i("TagA", "查找已加入的群失败");
    }

    @Override
    public void onJoinGroupSuccess() {
        runOnUiThread(() -> {
            Toast.makeText(this, "加群成功", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onJoinGroupFailed(String error, int code) {
        runOnUiThread(() -> {
            //判断是否已经加入过该群,601的错误码是已经加入过
            if (code == 601) {
                Toast.makeText(this, "已经加入过该群,无法重复加入", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "加群失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initData() {
        presenter = new AddGroupPresenter();
    }

    @Override
    protected void initView() {
        groupName = findViewById(R.id.addGroup_name);
        groupDesc = findViewById(R.id.addGroup_desc);
        createGroupBtn = findViewById(R.id.addGroup_createGroup_btn);

        createGroupBtn.setOnClickListener(v -> {
            EMGroupOptions option = new EMGroupOptions();
            option.maxUsers = 100;
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
            presenter.createGroup(groupName.getText().toString(), groupDesc.getText().toString(),
                    new String[]{EMClient.getInstance().getCurrentUser()}, "这是创建群", option);
        });
        findPublicGroupBtn = findViewById(R.id.addGroup_findPublicGroup);
        findPublicGroupBtn.setOnClickListener(v -> {
            presenter.findPublicGroups(10, null);
        });

        //初始化rv
        recyclerView = findViewById(R.id.addGroup_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        addGroupAdapter = new AddGroupAdapter(groupInfoList);
        recyclerView.setAdapter(addGroupAdapter);

        addGroupAdapter.setOnAddClick(groupId -> {
            //触发P层的加群
            presenter.joinGroup(groupId);
        });

        findJoinedGroupBtn = findViewById(R.id.addGroup_findJoinedGroup);
        findJoinedGroupBtn.setOnClickListener(v -> {
            presenter.findJoinedGroups();
        });
        findJoinedGroupAdapter = new FindJoinedGroupAdapter(groupList);
        findJoinedGroupAdapter.setOnJoinClick(groupId -> {
            Intent intent = new Intent(this, ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", groupId);
            bundle.putInt("chatType", 2);
            intent.putExtras(bundle);
            startActivity(intent);
        });
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_add_group;
    }
}